#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include<linux/in.h>
#include<stdlib.h>
#include<string.h>
#define N 128

int main(int argc, const char *argv[])
{
	int sockfd,acceptfd;
	sockfd=socket(AF_INET,SOCK_STREAM,0);
	if(sockfd<0)
	{
		perror("socket error");
		exit(-1);
	}
	printf("socket success\n");

	struct sockaddr_in serveraddr;
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr=inet_addr(argv[1]);

	socklen_t addrlen=sizeof(serveraddr);

	if(bind(sockfd,(struct sockaddr *)&serveraddr,addrlen)<0)
	{
		perror("bind error");
		exit(-1);
	}
	printf("bind success\n");

	if(listen(sockfd,8)<0)
	{
		perror("listen error");
		exit(-1);
	}
	printf("listen success\n");

	acceptfd=accept(sockfd,NULL,NULL);
	if(acceptfd<0)
	{
		perror("accept error");
		exit(-1);
	}
	printf("accept success\n");

	char buf[N]={0};
	char temp[N]={0};
	int recvbytes=0;
	while(1)
	{
		printf("wait recv\n");
		recvbytes=recv(acceptfd,(void *)buf,sizeof(buf),0);
		if(recvbytes<0)
		{
			perror("recv error");
			exit(-1);
		}else if(recvbytes==0)
		{
			break;
		}
		else
		{
			printf("recv data:%s\n",buf);
			sprintf(temp,"***!***%s",(char *)buf);
    		send(acceptfd,temp,sizeof(temp),0);
		}
	}
	

	return 0;
}
